package com.baizhi.controller;

import com.baizhi.Phone;
import com.baizhi.service.PhoneService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/page")
public class PhoneController {
    @Autowired
    private PhoneService ps;
    @RequestMapping("pageInfo")
    public String page(Model model, @RequestParam(required = false,defaultValue = "1") int pageIndex, @RequestParam(required = false,defaultValue = "2") int pageSize){
        PageInfo<Phone> phonePageInfo = ps.selectPage(pageIndex, pageSize);
        model.addAttribute("a",phonePageInfo);
        System.out.println(phonePageInfo);
        return "forward:/phoneList.jsp";
    }
    @RequestMapping("insert")
    public String insert(Phone phone){
        ps.addPhone(phone);
        return "redirect:/page/pageInfo.do";
    }
    @RequestMapping("delete")
    public String s(int id){
        ps.deletePhone(id);
        return "redirect:/page/pageInfo.do";
    }
    @RequestMapping("showPhone")
    public String show(int id,Model model){
        Phone select = ps.select(id);
        Model id1 = model.addAttribute("id", select);
        return "forward:/brand/finds.do";
    }
    @RequestMapping("update")
    public  String update(Phone p,int id){
        boolean update = ps.update(p, id);
        return "redirect:/page/pageInfo.do";
    }
    @RequestMapping("/updates")
    public String  update(Integer[] id ){
        List<Integer> integers = Arrays.asList(id);
        ps.deleteAll(integers);
        return "redirect:/page/pageInfo.do";
    }
}
